package cn.zysuyuan.item.controller;

import cn.zysuyuan.common.entity.PageResult;
import cn.zysuyuan.common.entity.R;
import cn.zysuyuan.item.entity.Company;
import cn.zysuyuan.item.service.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2019/10/29 13:53
 */

@Api(tags = "药材公司管理")
@RestController
@RequestMapping("company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @ApiOperation(value = "种植公司管理", notes = "列表管理")
    @GetMapping("companyData")
    public ResponseEntity<PageResult<Company>> select(@RequestParam Map<String,Object> map) {
        PageResult<Company> result = this.companyService.selectList(map);
//        if (CollectionUtils.isEmpty(result.getRows())){
//            return ResponseEntity.notFound().build();
//        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "查看所有公司名称", notes = "公司名称列表")
    @GetMapping("companyName")
    public ResponseEntity<PageResult<Company>> selectcName(){
        PageResult<Company> result = this.companyService.selectcName();
        return  ResponseEntity.ok(result);
    }

    @ApiOperation(value = "保存公司信息",notes = "保存公司信息")
    @PostMapping("companyData")
    public R save(@RequestBody Company company) {
        System.out.println("开始保存....");
        if (this.companyService.insert(company) > 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("flag", true);
            return R.ok(map);
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "删除公司信息",notes = "删除公司信息")
    @DeleteMapping("companyData")
    public R delete(@RequestParam("id") String id){
        System.out.println("开始删除....");
        if (this.companyService.delete(id) > 0){
            Map<String,Object> map = new HashMap<>();
            map.put("flag",true);
            return R.ok(map);
        }else{
            return R.error();
        }
    }

    @ApiOperation(value = "修改公司信息", notes = "修改公司信息")
    @PutMapping("companyData")
    public R update(@RequestBody Company company) {
        System.out.println("开始修改...");
        if (this.companyService.update(company) > 0) {
            Map<String,Object> map = new HashMap<>();
            map.put("flag",true);
            return R.ok(map);
        }else{
            return R.error();
        }
    }
}
